package com.zzyl.serve.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.NursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-20
 */
@Service
@Slf4j
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    @Autowired
    private NursingProjectPlanMapper NursingProjectPlanMapper;


    @Autowired
    NursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
         NursingPlanVo nursingPlanVo = new NursingPlanVo();
        /*return nursingPlanMapper.selectNursingPlanById(id);*/
        NursingPlan nursingPlan = this.getById(id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        List<NursingProjectPlan> projectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,id));
        List<NursingProjectPlanVo> projectPlanVos = projectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setPlanId(projectPlan.getPlanId());
            nursingProjectPlanVo.setProjectId(projectPlan.getProjectId()+"");
            nursingProjectPlanVo.setId(projectPlan.getId());
            nursingProjectPlanVo.setExecuteTime(projectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(projectPlan.getExecuteCycle()+"");
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(projectPlan.getExecuteFrequency()));
            return nursingProjectPlanVo;
        }).toList();
        nursingPlanVo.setProjectPlans(projectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);

    }

    /**
     * 新增护理计划
     *
     * @param nursingPlandto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlandto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlandto,nursingPlan);
       /* nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        int rows= NursingProjectPlanMapper.addBatch(nursingPlandto.getProjectPlans(),nursingPlan.getId());*/

        this.save(nursingPlan);
        nursingPlandto.getProjectPlans().forEach(nursingProjectPlan ->
                nursingProjectPlan.setPlanId(nursingPlan.getId())
        );
        //System.out.println(nursingPlandto.getProjectPlans());

        boolean flag = nursingProjectPlanService.saveBatch(nursingPlandto.getProjectPlans());

        return flag?1:0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlandto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlandto)
    {
        //nursingPlan.setUpdateTime(DateUtils.getNowDate());
        //return nursingPlanMapper.updateNursingPlan(nursingPlan);

        List<NursingProjectPlan> projectPlans = nursingPlandto.getProjectPlans();
        if(!CollectionUtils.isEmpty(projectPlans)){
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlandto.getId()));
            projectPlans.forEach(ProjectPlan ->{
                ProjectPlan.setPlanId(nursingPlandto.getId());
            });
            nursingProjectPlanService.saveBatch(projectPlans);
        }

        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlandto,nursingPlan);
        return updateById(nursingPlan)?1:0;
    }


    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,id));
        return removeById(id)?1:0;
    }
}
